package com.zhaowb.springcloud.admin.rest;

import com.alibaba.fastjson.JSONObject;
import com.zhaowb.springcloud.admin.biz.GroupBiz;
import com.zhaowb.springcloud.admin.entity.Group;
import com.zhaowb.springcloud.admin.vo.AuthorityMenuTree;
import com.zhaowb.springcloud.admin.vo.GroupUsers;
import com.zhaowb.springcloud.common.msg.ObjectRestResponse;
import com.zhaowb.springcloud.common.rest.BaseController;
import io.swagger.models.auth.In;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IDEA
 * 群组模块
 *
 * @author zhaowb
 * @date 2018/11/7 10:31
 */
@Controller
@RequestMapping("group")
public class GroupController extends BaseController<GroupBiz, Group> {

    @ResponseBody
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public List<Group> list(String name, String groupType) {
        if (StringUtils.isBlank(name) && StringUtils.isBlank(groupType)) {
            return new ArrayList<>();
        }
        Example example = new Example(Group.class);
        if (StringUtils.isNotBlank(name)) {
            example.createCriteria().andLike("name", "%" + name + "%");
        }
        if (StringUtils.isNotBlank(groupType)) {
            example.createCriteria().andLike("groupType", groupType);
        }
        return baseBiz.selectByExample(example);
    }

    @ResponseBody
    @RequestMapping(value = "/{id}/user", method = RequestMethod.PUT)
    public ObjectRestResponse modifyUsers(@PathVariable("id") int id, String members, String leaders) {
        baseBiz.modifyGroupUsers(id, members, leaders);
        return new ObjectRestResponse().rel(true);
    }

    @ResponseBody
    @RequestMapping(value = "/{id}/user", method = RequestMethod.GET)
    public ObjectRestResponse<GroupUsers> getUsers(@PathVariable int id) {
        return new ObjectRestResponse<GroupUsers>().rel(true).result(baseBiz.getGroupUsers(id));
    }

    @ResponseBody
    @RequestMapping(value = "/{id}/authority/menu", method = RequestMethod.POST)
    public ObjectRestResponse modifyMenuAuthority(@PathVariable("id") int id, String menuTree) {
        List<AuthorityMenuTree> menus = JSONObject.parseArray(menuTree, AuthorityMenuTree.class);
        baseBiz.modifyAuthorityMenu(id, menus);
        return new ObjectRestResponse().rel(true);
    }

    @ResponseBody
    @RequestMapping(value = "/{id}/authority/menu", method = RequestMethod.GET)
    public ObjectRestResponse<List<AuthorityMenuTree>> getMenuAuthority(@PathVariable("id") int id) {
        return new ObjectRestResponse().rel(true).result(baseBiz.getAuthorityMenu(id));
    }

    @ResponseBody
    @RequestMapping(value = "/{id}/authority/element/add", method = RequestMethod.POST)
    public ObjectRestResponse addElementAuthority(@PathVariable("id") int id, int menuId, int elementId) {
        baseBiz.modifyAuthorityElement(id, menuId, elementId);
        return new ObjectRestResponse().rel(true);
    }

    @ResponseBody
    @RequestMapping(value = "/{id}/authority/element/remove", method = RequestMethod.POST)
    public ObjectRestResponse removeElementAuthority(@PathVariable("id") int id, int menuId, int elementId) {
        baseBiz.removeAuthorityElement(id, menuId, elementId);
        return new ObjectRestResponse().rel(true);
    }

    @ResponseBody
    @RequestMapping(value = "/{id}/authority/element", method = RequestMethod.GET)
    public ObjectRestResponse<List<Integer>> addElementAuthority(@PathVariable("id") int id) {
        return new ObjectRestResponse<>().rel(true).result(baseBiz.getAuthorityElement(id));
    }
}
